<?php
#############################################################################
# Author: consros 2010                                                      #
#############################################################################

require_once "settings.inc";
require_once "tools.inc";
require_once "lang.inc";
require_once "parsers.inc";

define("COOKIE_NAME", "EtvCookie");
define("CHANGE_SERVER_NAME", "ChangeServer");
define("BASE_URL", "http://etvnet.com");

class EtvFunctions {
    var $traces;
    var $cookie;

    function EtvFunctions($traces = false) {
        $this->traces = $traces;
        $this->cookie = isset($_SESSION[COOKIE_NAME]) ?
            $_SESSION[COOKIE_NAME] : "";
    }

    function forgetCookie() {
        $_SESSION[COOKIE_NAME] = "";
        $this->cookie = "";
    }

    function trace($msg) {
        if ($this->traces) {
            print "DEBUG: " . USERNAME . ": $msg\n";
        }
    }

    function authorize() {
        $this->trace("Authorization started");
        if ("" === USERNAME || "" === PASSWORD) {
            dieOnError(LANG_ERR_AUTHORIZATION, "img/errors/wrong-user.png");
        }
        $params  = "username=" . USERNAME;
        $params .= "&password=" . PASSWORD;
        $params .= "&next=" . urlencode("/");
        $url = BASE_URL . "/login/";
        $headerExt = "Accept: text/html\r\n";
        $html = getPageContentByPost($url, $params, $headerExt);
        $cookies = parseOutCookies($html);
        $this->cookie = 'Cookie: ' . implode('; ', $cookies);
        strpos($this->cookie, "sessid") or
            dieOnError(USERNAME . ": " . LANG_ERR_AUTHORIZATION,
                "img/errors/wrong-user.png");
        strpos($this->cookie, "=deleted") and
            dieOnError(USERNAME . ": " . LANG_ERR_SESSION_TIMEOUT,
                "img/errors/session-timeout.png");
        $this->trace("Authorization returned: $this->cookie");
        $_SESSION[COOKIE_NAME] = $this->cookie;
    }

    function isAuthorized($reply) {
        $ok = isset($this->cookie) &&
            0 != strlen($this->cookie) &&
            false == strpos($this->cookie, "deleted") &&
            false == strpos($reply, "login-frm") &&
            false == strpos($reply, "msg=access_denied") &&
            false == strpos($reply, "msg=auth_err");
        if (! $ok) {
            $this->trace("Authorization missed or lost");
        }
        return $ok;
    }

    function getData($url, $name) {
        $this->trace("Getting $name");
        $this->trace("getData $url, " . $this->cookie);

        $reply = getPageContentByGet($url, $this->cookie);
        if (! $this->isAuthorized($reply)) {
            $this->authorize();
            $this->trace("Second try to get $name");
            $reply = getPageContentByGet($url, $this->cookie);
            $this->isAuthorized($reply) or
                dieOnError(USERNAME . ": " . LANG_ERR_AUTHORIZATION,
                    "img/errors/wrong-user.png");
        }
        # $this->trace("getData result: $reply");
        return $reply;
    }

    function getStreamPage($path) {
        $url = BASE_URL . $path;
        return $this->getData($url, "URL of stream $path");
    }

    function getStreamUrl($path, $changeServer = false, $quality = 2) {
        $this->trace("getStreamUrl started, change server: " . $changeServer);
        if (! $this->isAuthorized("")) {
            $this->authorize();
        }

        $params  = "bitrate=";
        $params .= 3 == $quality ? "2&high_quality=" : $quality;
        $params .= "&view=submit";
        $params .= $changeServer ? "&other_server=1" : "";

        $url = BASE_URL . preg_replace('|^/.*?/|', '/watch/', $path);

        $headerExt  = $this->cookie . "\r\n";
        $headerExt .= "Referer: " . BASE_URL . "$path\r\n";
        $headerExt .= "Accept: application/json, text/javascript, */*\r\n";

        $reply = getPageContentByPost($url, $params, $headerExt);
        if (! $this->isAuthorized($reply)) {
            $this->authorize();
            $this->trace("Second try to get getStreamUrl: " . $this->cookie);
            $reply = getPageContentByPost($url, $params, $headerExt);
            $this->isAuthorized($reply) or
                dieOnError(LANG_ERR_REQUEST_SENDING . ": $name:\n$reply",
                    "img/errors/wrong-xml.png");
        }
        $this->trace("getStreamUrl result: $reply");
        return $reply;
    }

    function getShortStreamUrl($path, $quality = 2, $tries = 5) {
        $this->trace("getShortStreamUrl started: " . $this->cookie);
        $url = "";
        while ($tries-- > 0 && ! preg_match('|\.wmv$|', $url)) {
            $html = $this->getStreamUrl($path, true, $quality);
            $url = parseUrl($html);
            $this->trace("getStreamUrl result (left $tries): $url");
        }
        return $url;
    }
}
?>
